﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.DataAccess;

namespace YiYiTong.Rocket.Business
{
    public class ActionBiz
    {
        private static readonly ModelHandler<ActionInfo> modelHelper = new ModelHandler<ActionInfo>();

        #region Instance
        private ActionBiz()
        {
        }
        private static ActionBiz _instance;

        private static object _syncObject = new object();

        public static ActionBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new ActionBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion


        /// <summary>
        /// 添加action
        /// </summary>
        /// <param name="actionName">action名称</param>
        /// <param name="controllName">控制器名称</param>
        /// <param name="remark">备注</param>
        /// <param name="parentId">父节点ID</param>
        /// <param name="strOutPut"></param>
        /// <returns></returns>
        public int AddAction(string actionName, string controllName, string description, string remark, int parentId, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "添加功能失败";
            try
            {
                if (!string.Equals(actionName, "#"))
                {
                    DataTable dt = ActionDataAccess.Instance.GetActionInfo(actionName, controllName);
                    if (dt != null && dt.Rows.Count != 0)
                    {
                        ret = -1;
                        strOutPut = "功能名称已存在";
                        return ret;
                    }
                }
                string createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ret = ActionDataAccess.Instance.AddAction(actionName, controllName, description, createTime, 0, remark, parentId);
                if (ret > 0)
                {
                    strOutPut = "添加功能成功";
                }
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 修改action
        /// </summary>
        /// <param name="actionId">action编号</param>
        /// <param name="actionName">action名称</param>
        /// <param name="controllName">控制器名称</param>
        /// <param name="remark">备注</param
        /// <param name="parentId">父节点ID</param>
        /// <param name="strOutPut"></param>
        /// <returns></returns>
        public int EditAction(int actionId, string actionName, string controllName, string description, string remark, int parentId, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "修改功能失败";
            try
            {
                DataTable dt = ActionDataAccess.Instance.GetActionInfo(actionName, controllName);
                if (dt != null && dt.Rows.Count != 0)
                {
                    if (parentId == ConvertHelper.ConvertTo<int>(dt.Rows[0]["ParentID"]))
                    {
                        ret = -1;
                        strOutPut = "功能名称已存在";
                        return ret;
                    }
                    //ret = ActionDataAccess.Instance.EditAction(actionId, controllName, description, remark, parentId);
                }
                ret = ActionDataAccess.Instance.EditAction(actionId, actionName, controllName, description, remark, parentId);
                if (ret > 0)
                    strOutPut = "修改功能成功";
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 删除action
        /// </summary>
        /// <param name="actionId">action编号</param>
        /// <param name="strOutPut"></param>
        /// <returns></returns>
        public int DeleteAction(int actionId, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "删除功能失败";
            try
            {
                DataTable dt = ActionDataAccess.Instance.GetActionInfo(actionId);
                if (dt == null && dt.Rows.Count == 0)
                {
                    ret = -1;
                    strOutPut = "删除功能失败,功能不存在";
                    return ret;
                }
                else
                {
                    ret = ActionDataAccess.Instance.DeleteAction(actionId);
                }

                if (ret > 0)
                {
                    strOutPut = "删除功能成功";
                }
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 根据条件查询action信息
        /// </summary>
        /// <param name="actionName">action名称</param>
        /// <param name="controllName">控制器名称</param>
        /// <param name="appId">区分不同平台用户权限</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageRows">每页行数</param>
        /// <returns></returns>
        public DataSet GetPageList(string actionName, string controllName, string description, string parentName, int pageIndex, int pageRows)
        {
            return ActionDataAccess.Instance.GetPageList(actionName, controllName, description, parentName, pageIndex, pageRows);
        }

        /// <summary>
        /// 根据action编号获取action信息
        /// </summary>
        /// <param name="actionId">action编号</param>
        /// <returns></returns>
        public ActionInfo GetActionInfoById(int actionId)
        {
            ActionInfo action = new ActionInfo();
            DataTable dt = ActionDataAccess.Instance.GetActionInfo(actionId);
            if (dt != null && dt.Rows.Count != 0)
            {
                action = modelHelper.FillModel(dt.Rows[0]);
            }
            return action;
        }

        /// <summary>
        /// 获取所有action信息
        /// </summary>
        /// <param name="appId">区分不同平台用户权限</param>
        /// <returns></returns>
        public DataTable GetAllActionList()
        {
            return ActionDataAccess.Instance.GetAllActionList();
        }

        /// <summary>
        /// 获取所有父功能action信息
        /// </summary>
        /// <param name="appId">区分不同平台用户权限</param>
        /// <returns></returns>
        public DataTable GetAllParentActionList()
        {
            return ActionDataAccess.Instance.GetAllParentActionList();
        }
    }
}